﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Firestarter.DB.Schema.UI.RevEngDBWizard;
using FireStarterModeller.Test;
using Habanero.Base;
using Habanero.Faces.Base;
using Habanero.Test;
using NUnit.Framework;

namespace Firestarter.DB.Schema.Test.UI
{
    [TestFixture]
    public class TestOpenFileDialogExtendedTextBoxMapper : TestBase
    {
        private static string _propertyName = "ShapeName";
        [Test]
// ReSharper disable InconsistentNaming
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            var testDialog = new OpenFileDialogExtendedTextBox();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var mapper =
                new OpenFileDialogExtendedTextBoxMapper(testDialog, _propertyName, false,
                                                        GlobalUIRegistry.ControlFactory);
            //---------------Test Result -----------------------
            Assert.IsNotNull(mapper);
            Assert.AreEqual(_propertyName, mapper.PropertyName, "Property Name should match property name inputted in constructor");
            Assert.IsInstanceOf<OpenFileDialogExtendedTextBox>(mapper.Control);
        }

        [Test]
        public void Test_BusinessObject_WhenSet_ShouldReturnValidBusinessObject()
        {
            //---------------Set up test pack-------------------
            var mapper = CreateOpenFileDialogExtendedTextBoxMapper(CreateOpenFileDialogExtendedTextBox());
            var shape = new Shape();
            //---------------Assert Precondition----------------
            Assert.IsInstanceOf<IBusinessObject>(shape);
            Assert.IsNull(mapper.BusinessObject);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = shape;
            //---------------Test Result -----------------------
            Assert.IsInstanceOf<IBusinessObject>(mapper.BusinessObject);
            Assert.AreSame(shape, mapper.BusinessObject);
            Assert.IsNotNull(mapper.BusinessObject);
        }

        [Test]
        public void Test_PropertyNameOnBO_WhenSet_ShouldUpdateTextBox_Text()
        {
            //---------------Set up test pack-------------------
            var testDialog = CreateOpenFileDialogExtendedTextBox();
            var mapper = CreateOpenFileDialogExtendedTextBoxMapper(testDialog);
            var testShapeName = GetRandomString();
            var shape = new Shape {ShapeName = testShapeName};
            //---------------Assert Precondition----------------
            Assert.AreEqual(testShapeName, shape.ShapeName);
            //---------------Execute Test ----------------------
            mapper.BusinessObject  = shape;
            //---------------Test Result -----------------------
            Assert.AreEqual(testShapeName, testDialog.TextBox.Text);
        }

        [Test]
        public void Test_TextBox_Text_WhenSet_ShouldUpdateBusinessObject()
        {
            //---------------Set up test pack-------------------
            var testDialog = CreateOpenFileDialogExtendedTextBox();
            var mapper = CreateOpenFileDialogExtendedTextBoxMapper(testDialog);
            var testShapeName = GetRandomString();
            var testTextBoxString = GetRandomString();
            var shape = new Shape { ShapeName = testShapeName };

            mapper.BusinessObject = shape;
            //---------------Assert Precondition----------------
            Assert.AreEqual(testShapeName, shape.ShapeName);
            //---------------Execute Test ----------------------
            testDialog.TextBox.Text = testTextBoxString;
            mapper.ApplyChangesToBusinessObject();
            //---------------Test Result -----------------------
            Assert.AreEqual(testTextBoxString, shape.ShapeName);
        }

        [Test]
        public void Test_TextBox_Text_WhenSetAndBusinessObjectNotSetOnMapper_ShouldNotUpdateBusinessObject()
        {
            //---------------Set up test pack-------------------
            var testDialog = CreateOpenFileDialogExtendedTextBox();
            var mapper = CreateOpenFileDialogExtendedTextBoxMapper(testDialog);
            var testShapeName = GetRandomString();
            var testTextBoxString = GetRandomString();
            var shape = new Shape { ShapeName = testShapeName };
            //---------------Assert Precondition----------------
            Assert.AreEqual(testShapeName, shape.ShapeName);
            Assert.IsNull(mapper.BusinessObject);
            Assert.AreNotSame(shape, mapper.BusinessObject);
            //---------------Execute Test ----------------------
            testDialog.TextBox.Text = testTextBoxString;
            mapper.ApplyChangesToBusinessObject();
            //---------------Test Result -----------------------
            Assert.AreNotEqual(testDialog.TextBox.Text, shape.ShapeName);
        }

        [Test]
        public void Test_BusinessObject_WhenSetToAnotherBusinessObject_ShouldUpdateMappedControlToTheNewBO()
        {
            //---------------Set up test pack-------------------
            var testDialog = CreateOpenFileDialogExtendedTextBox();
            var mapper = CreateOpenFileDialogExtendedTextBoxMapper(testDialog);
            var testShapeName = GetRandomString();
            var testShapeName2 = GetRandomString();
            var shape = new Shape { ShapeName = testShapeName };
            var shape2 = new Shape { ShapeName = testShapeName2 };
            mapper.BusinessObject = shape;
            //---------------Assert Precondition----------------
            Assert.AreEqual(testShapeName, testDialog.TextBox.Text);
           Assert.AreSame(shape, mapper.BusinessObject);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = shape2;
            //---------------Test Result -----------------------
            Assert.AreNotEqual(testShapeName, testDialog.TextBox.Text);
            Assert.AreEqual(testShapeName2, testDialog.TextBox.Text);
            Assert.AreSame(shape2, mapper.BusinessObject);
            Assert.AreNotSame(shape,mapper.BusinessObject);
        }
        // ReSharper restore InconsistentNaming

        private static OpenFileDialogExtendedTextBoxMapper CreateOpenFileDialogExtendedTextBoxMapper(OpenFileDialogExtendedTextBox openFileDialogExtendedTextBox)
        {
            
            return new OpenFileDialogExtendedTextBoxMapper(openFileDialogExtendedTextBox, _propertyName, false,
                                                           GlobalUIRegistry.ControlFactory);
        }

        private static OpenFileDialogExtendedTextBox CreateOpenFileDialogExtendedTextBox()
        {
            return new OpenFileDialogExtendedTextBox();
        }

        private static string GetRandomString()
        {
            return TestUtilsShared.GetRandomString();
        }
    }
}
